/**
 * Created by Administrator on 2015/11/27.
 */

/**
 * User Object
 * @param options
 * @constructor
 */
var User = function (options) {
    var $self = this
    $self.id = options && options.id || null
    $self.username = options && options.username || null
    $self.password = options && options.password || null
    $self.nick = options && options.nick || null
    $self.gender = options && options.gender || true
    $self.telephone = options && options.telephone || null
    $self.qq = options && options.qq || null
    $self.birthday = options && options.birthday || null
    $self.address = options && options.address || null
    $self.portrait = options && options.portrait || '/images/portrait.gif'
    $self.craftwork = options && options.craftwork || null
    $self.flavour = options && options.flavour || null
    $self.createOn = options && options.createOn || new Date()
}

User.prototype.up$recipe = function *(ctx) {
    yield ctx.db('UPDATE User SET recipeCount = recipeCount +1 WHERE id=?', this.id).execute()
}
User.prototype.up$menu = function *(ctx) {
    yield ctx.db('UPDATE User SET menuCount = menuCount +1 WHERE id=?', this.id).execute()
}
User.prototype.up$focus = function *(ctx) {
    yield ctx.db('UPDATE User SET focusCount = focusCount +1 WHERE id=?', this.id).execute()
}
User.prototype.up$recipe = function *(ctx) {
    yield ctx.db('UPDATE User SET fansCount = fansCount +1 WHERE id=?', this.id).execute()
}
User.prototype.has$collect$recipe = function *(ctx, rid) {
    var count = yield ctx.db('SELECT COUNT(*) FROM RecipeCollect WHERE rid=? AND uid=?', [rid, this.id]).count()
    return count > 0;
}
User.prototype.has$collect$menu = function *(ctx, mid) {
    var count = yield ctx.db('SELECT COUNT(*) FROM Menu_Collect WHERE m_id=? AND r_id=?', [mid, this.id]).count()
    return count > 0;
}
User.prototype.has$upvote$recipe = function *(ctx, rid) {
    var count = yield ctx.db('SELECT COUNT(*) FROM RecipeUpvote WHERE rid=? AND uid=?', [rid, this.id]).count()
    return count > 0;
}
User.prototype.has$upvote$menu = function *(ctx, mid) {
    var count = yield ctx.db('SELECT COUNT(*) FROM Menu_Upvote WHERE m_id=? AND r_id=?', [mid, this.id]).count()
    return count > 0;
}

/**
 * Recipe Object
 * @param options
 * @constructor
 */
var Recipe = function (options) {
    var $self = this
    $self.id = options && options.id || null
    $self.title = options && options.title || null
    $self.finishedProductImage = options && options.finishedProductImage || null
    $self.backgroundStory = options && options.backgroundStory || null
    $self.ingredient = options && JSON.stringify(options.ingredient) || null
    $self.seasoning = options && JSON.stringify(options.seasoning) || null
    $self.step = options && JSON.stringify(options.step) || null
    $self.knack = options && options.knack || null
    $self.keyword = options && options.keyword || null
    $self.isRaw = options && options.isRaw || true
    $self.comment = options && options.comment || 0
    $self.upvote = options && options.upvote || 0
    $self.createOn = options && options.createOn || new Date()
    $self.collect = options && options.collect || 0 //2015-12-02 10:38:40 ztq 数据新添的字段

    $self.category_id = options && parseInt(options.category_id) || null
    $self.cookie_level_id = options && parseInt(options.cookie_level_id) || null
    $self.cookie_diff_id = options && parseInt(options.cookie_diff_id) || null
    $self.cookie_spend_id = options && parseInt(options.cookie_spend_id) || null
    $self.craftwork_id = options && parseInt(options.craftwork_id) || null
    $self.flavour_id = options && parseInt(options.flavour_id) || null
    $self.owner_id = options && parseInt(options.owner_id) || null
}
/**
 *  # 动态信息日志表
 *  # 1 -> 谁谁谁评论了你的菜单
 *  # 2 -> 谁谁谁评论了你的菜谱
 *  # 3 -> 谁谁谁回复了你的评论
 *  # 4 -> 谁谁谁关注了你
 *  # 5 -> 谁谁谁取消了对你的关注
 *  # 6 -> 谁谁点赞了你的菜谱
 *  # 7 -> 谁谁点赞了你的菜单
 *  # 8 -> 谁谁点赞了你的评论
 *  # 9 -> 谁谁反对了你的评论
 * @param options
 * @constructor
 */
var Message = function (options) {
    var $self = this
    $self.id = options && options.id || null
    $self.user_id = options && options.user_id || null
    $self.another_id = options && options.another_id || null
    $self.obj_id = options && options.obj_id || null
    $self.type = options && options.type || 1
    $self.another_already_read = options && options.another_already_read || 0
}
Message.TYPE_COMMENT_MENU = 1
Message.TYPE_COMMENT_RECIPE = 2
Message.TYPE_REPLY_COMMENT = 3
Message.TYPE_FOCUS_YOU = 4
Message.TYPE_CENCEL_FOCUS_YOU = 5
Message.TYPE_UPVOTE_RECIPE = 6
Message.TYPE_UPVOTE_MENU = 7
Message.TYPE_UPVOTE_COMMENT = 8
Message.TYPE_DOWNVOTE_COMMENT = 9

/**
 * 评论
 * # 1 -> 评论菜谱
 * # 2 -> 评论菜单
 * # 3 -> 回复评论
 * @param options
 * @constructor
 */
var Comment = function (options) {
    var $self = this
    $self.id = options && options.id || null
    $self.user_id = options && options.user_id || null
    $self.obj_id = options && options.obj_id || null
    $self.type = options && options.type || null
    $self.content = options && options.content || null
    $self.createOn = options && options.createOn || new Date()
    $self.upvote = options && options.upvote || 0
    $self.downvote = options && options.downvote || 0
}
Comment.TYPE_RECIPE = 1
Comment.TYPE_MENU = 2
Comment.TYPE_REPLY = 3

var Menu = function (options) {
    var $self = this
    $self.id = options && options.id || null
    $self.name = options && options.name || null
    $self.user_id = options && options.user_id || null
    $self.description = options && options.description || null
    $self.upvote = options && options.upvote || true
    $self.collectionCount = options && options.collectionCount || null
    $self.createOn = options && options.createOn || new Date()
}


/**
 * 分页对象
 * @param pageNum current page number
 * @param pageSize how many object in each page
 * @param recordCount how many record in database
 * @param data the data collection
 * @constructor
 */
var Page = function (pageNum, pageSize, recordCount, data) {
    var $self = this
    var PAGE_NAVIGATE = 5
    if(arguments.length>4){
        console.log('bad constructor!!')
        return
    }
    $self.pageNum = pageNum || 1
    $self.pageSize = pageSize || 25
    $self.recordCount = recordCount || 0
    $self.data = data || []
    $self.pageCount = parseInt(($self.recordCount + $self.pageSize - 1) / $self.pageSize)
    var HALF_PAGE_NAVIGATE = parseInt(PAGE_NAVIGATE/2)

    if ($self.pageCount <= PAGE_NAVIGATE){
        $self.begin = 1;
        $self.end = $self.pageCount;
    }else if ($self.pageNum <= HALF_PAGE_NAVIGATE){
        $self.begin = 1;
        $self.end = PAGE_NAVIGATE;
    }else if ($self.pageNum + HALF_PAGE_NAVIGATE > $self.pageCount){
        $self.begin = $self.pageCount - (PAGE_NAVIGATE - 1);
        $self.end = $self.pageCount;
    }else {
        $self.begin = $self.pageNum - HALF_PAGE_NAVIGATE;
        $self.end = $self.pageNum + HALF_PAGE_NAVIGATE;
    }
}

module.exports = {
    User: User,
    Menu: Menu,
    Recipe: Recipe,
    Message: Message,
    Page: Page
}